<?php
class VideosAdvicesPointsController extends AppController{
    
    /**
     *  
     * @param type $videosAdviceAnswerId
     * @param type $value 
     * 
     *  TODO : verification du bon déroulement de l'enregistrement
     */
    public function addPoint($videosAdviceAnswerId, $value){
        
        
        // Vérification que l'utilisateur a pas déja voté 
        if (!$this->hasVoted($videosAdviceAnswerId)){
        
            // Insertion du vote
            if ($value == 1){
                $this->VideosAdvicesPoint->VideosAdvicesAnswer->increment('pros', $videosAdviceAnswerId);
            }
            else{
                $this->VideosAdvicesPoint->VideosAdvicesAnswer->increment('cons', $videosAdviceAnswerId);
            }

            //$this->VideosAdvicesPoint->VideosAdvicesAnswer->save($data);

            $data = array(
                'value' => $value,
                'videos_advices_answers_id' => $videosAdviceAnswerId,
                'users_id' => $this->getCurrentUserId()
            );

            $data =  $this->VideosAdvicesPoint->save($data);
        }
        
        $this->redirect($this->referer());
        
    }
    
    public function addPros($videosAdviceAnswerId){
        $this->addPoint($videosAdviceAnswerId, 1);
    }
    
    public function addCons($videosAdviceAnswerId){
        $this->addPoint($videosAdviceAnswerId, -1);
    }
    
    public function hasVoted($videosAdvicesAnswerId){
        return $this->VideosAdvicesPoint->find('count', array(
            'conditions' => array(
                'videos_advices_answers_id' => $videosAdvicesAnswerId,
                'users_id' => $this->getCurrentUserId()
            ),
            'recursive' => -1
        ));
    }
    
}
?>
